/**
 * Title : jFamilyTree
 * Description: jFamilyTree
 * About: Designed and rewritten by Andreas Lundgren, based on the Treebolic application suite
 * Copyright:	(c) 2001-2008
 * Terms of use:see license agreement at http://treebolic.sourceforge.net/en/license.htm
 * Author: Bernard Bou
 * Company: bsys
 */
package jFamilyTree.commons;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Properties;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.ListCellRenderer;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * Site dialog
 * 
 * @author Bernard Bou
 */
public class SiteDialog extends JDialog
{
	private static final long serialVersionUID = 1L;

	/**
	 * Mode types
	 */
	static private enum Mode
	{
		FILE, NET
	}

	// V A L U E S
	
	/**
	 * Properties (input/output)
	 */
	private Properties theProperties;

	/**
	 * Result
	 */
	public boolean ok;

	// C O M P O N E N T S

	/**
	 * Combo box
	 */
	private JComboBox theComboBox;

	/**
	 * Server field
	 */
	private JTextField theServerTextField;

	/**
	 * Directory field
	 */
	private JTextField theDirectoryTextField;

	/**
	 * Login field
	 */
	private JTextField theLoginTextField;

	/**
	 * Password field
	 */
	private JPasswordField thePasswordTextField;

	/**
	 * Path field
	 */
	private JTextField thePathTextField;

	// C O N S T R U C T OR

	/**
	 * Constructor
	 */
	public SiteDialog(Properties theseProperties)
	{
		theProperties = theseProperties;
		initialize();
	}

	/**
	 * Initialize dialog
	 */
	private void initialize()
	{
		setTitle(Messages.getString("SiteDialog.0")); //$NON-NLS-1$
		setResizable(true);

		// images
		Icon thisIcon = new ImageIcon(SiteDialog.class.getResource("images/sitemake.png")); //$NON-NLS-1$
		JLabel thisHeaderLabel = new JLabel();
		thisHeaderLabel.setIcon(thisIcon);
		thisHeaderLabel.setVerticalTextPosition(JLabel.TOP);
		thisHeaderLabel.setHorizontalTextPosition(JLabel.CENTER);
		thisHeaderLabel.setFont(new Font("Dialog", Font.BOLD, 16)); //$NON-NLS-1$
		thisHeaderLabel.setText(Messages.getString("SiteDialog.1")); //$NON-NLS-1$
		thisHeaderLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
		final Icon thisFileIcon = new ImageIcon(SiteDialog.class.getResource("images/local.png"));
		final Icon thisHttpIcon = new ImageIcon(SiteDialog.class.getResource("images/net.png"));

		// mode combo
		Object[] theseOptions = { Mode.FILE, Mode.NET };
		theComboBox = new JComboBox(theseOptions);
		theComboBox.setRenderer(new ListCellRenderer()
		{
			private JLabel theLabel = new JLabel();

			public Component getListCellRendererComponent(JList thisList, Object thisValue, int thisIndex, boolean thisIsSelected, boolean thisCellHasFocus)
			{
				theLabel.setText(thisValue.toString());
				theLabel.setIcon(thisValue.equals(Mode.FILE) ? thisFileIcon : thisHttpIcon);
				return theLabel;
			}
		});

		// text fields
		theServerTextField = new JTextField(16);
		theDirectoryTextField = new JTextField(16);
		theLoginTextField = new JTextField(16);
		thePasswordTextField = new JPasswordField(16);
		thePathTextField = new JTextField(16);

		// buttons
		JButton thisOKButton = new JButton(Messages.getString("SiteDialog.2")); //$NON-NLS-1$
		JButton thisCancelButton = new JButton(Messages.getString("SiteDialog.3")); //$NON-NLS-1$
		JButton thisBrowsePathButton = new JButton(Messages.getString("SiteDialog.4")); //$NON-NLS-1$

		// panels
		JPanel thisSelectionPanel = new JPanel();
		thisSelectionPanel.add(new JLabel(Messages.getString("SiteDialog.5"))); //$NON-NLS-1$
		thisSelectionPanel.setLayout(new FlowLayout());
		thisSelectionPanel.add(theComboBox);

		JPanel thePathPanel = new JPanel();
		thePathPanel.setLayout(new FlowLayout());
		thePathPanel.add(new JLabel(Messages.getString("SiteDialog.30"))); //$NON-NLS-1$
		thePathPanel.add(thePathTextField);

		JPanel theFilePanel = new JPanel();
		theFilePanel.setLayout(new FlowLayout());
		theFilePanel.add(thePathPanel);
		theFilePanel.add(thisBrowsePathButton);

		JPanel theFTPPanel = new JPanel();
		theFTPPanel.setLayout(new GridBagLayout());
		theFTPPanel.add(new JLabel(Messages.getString("SiteDialog.31")), new GridBagConstraints(0, 0, 1, 1, 0., 0., GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 20, 0, 10), 0, 0)); //$NON-NLS-1$
		theFTPPanel.add(new JLabel(Messages.getString("SiteDialog.32")), new GridBagConstraints(0, 1, 1, 1, 0., 0., GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 20, 0, 10), 0, 0)); //$NON-NLS-1$
		theFTPPanel.add(new JLabel(Messages.getString("SiteDialog.33")), new GridBagConstraints(0, 2, 1, 1, 0., 0., GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 20, 0, 10), 0, 0)); //$NON-NLS-1$
		theFTPPanel.add(new JLabel(Messages.getString("SiteDialog.34")), new GridBagConstraints(0, 3, 1, 1, 0., 0., GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 20, 0, 10), 0, 0)); //$NON-NLS-1$
		theFTPPanel.add(theServerTextField, new GridBagConstraints(1, 0, 1, 1, 1., 0., GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 0, 20), 0, 0));
		theFTPPanel.add(theDirectoryTextField, new GridBagConstraints(1, 1, 1, 1, 1., 01., GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 0, 20), 0, 0));
		theFTPPanel.add(theLoginTextField, new GridBagConstraints(1, 2, 1, 1, 1., 0., GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 0, 20), 0, 0));
		theFTPPanel.add(thePasswordTextField, new GridBagConstraints(1, 3, 1, 1, 1., 0., GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 0, 20), 0, 0));

		JPanel thisButtonPanel = new JPanel();
		thisButtonPanel.setLayout(new FlowLayout());
		thisButtonPanel.add(thisCancelButton);
		thisButtonPanel.add(thisOKButton);

		// tab init
		final JTabbedPane thisTabbedPanel = new JTabbedPane();
		thisTabbedPanel.addTab(Mode.FILE.toString(), thisFileIcon, theFilePanel, Messages.getString("SiteDialog.20")); //$NON-NLS-1$
		thisTabbedPanel.addTab(Mode.NET.toString(), thisHttpIcon, theFTPPanel, Messages.getString("SiteDialog.21")); //$NON-NLS-1$

		// connect selections
		thisTabbedPanel.addChangeListener(new ChangeListener()
		{
			public void stateChanged(ChangeEvent thisE)
			{
				int thisIndex = thisTabbedPanel.getSelectedIndex();
				theComboBox.setSelectedIndex(thisIndex);
			}
		});
		theComboBox.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent thisE)
			{
				int thisIndex = theComboBox.getSelectedIndex();
				thisTabbedPanel.setSelectedIndex(thisIndex);
			}
		});

		// assemble tabs
		JPanel thisSettingsPanel = new JPanel();
		thisSettingsPanel.add(thisTabbedPanel);

		// events
		thisBrowsePathButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				String thisPath = FileDialogs.getFolder(theProperties.getProperty("base", ".")); //$NON-NLS-1$ //$NON-NLS-2$
				if (thisPath != null)
				{
					thePathTextField.setText(thisPath);
				}
			}
		});
		thisOKButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				ok = true;
				setVisible(false);
			}
		});
		thisCancelButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				setVisible(false);
			}
		});
		
		// assemble
		JPanel thisPanel = new JPanel();
		thisPanel.setLayout(new BoxLayout(thisPanel, BoxLayout.Y_AXIS));
		thisPanel.add(Box.createRigidArea(new Dimension(0, 10)));
		thisPanel.add(thisHeaderLabel);
		thisPanel.add(thisSelectionPanel);
		thisPanel.add(thisSettingsPanel);
		thisPanel.add(thisButtonPanel);
		thisPanel.add(Box.createRigidArea(new Dimension(0, 10)));

		setContentPane(thisPanel);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.Dialog#setVisible(boolean)
	 */
	public void setVisible(boolean thisFlag)
	{
		if (thisFlag)
		{
			ok = false;

			// read properties into components
			theServerTextField.setText(theProperties.getProperty("server")); //$NON-NLS-1$
			theDirectoryTextField.setText(theProperties.getProperty("directory")); //$NON-NLS-1$
			theLoginTextField.setText(theProperties.getProperty("login")); //$NON-NLS-1$
			thePasswordTextField.setText(theProperties.getProperty("password")); //$NON-NLS-1$
			thePathTextField.setText(theProperties.getProperty("path")); //$NON-NLS-1$

			Mode thisMode = theProperties.getProperty("mode") != null ? Mode.valueOf(theProperties.getProperty("mode")) : Mode.FILE; //$NON-NLS-1$ //$NON-NLS-2$
			theComboBox.setSelectedItem(thisMode);

			pack();
			Utils.center(this);
		}
		else
		{
			if (ok)
			{
				// update properties from components
				Mode thisMode = (Mode) theComboBox.getSelectedItem();
				theProperties.setProperty("mode", thisMode.toString()); //$NON-NLS-1$
				switch (thisMode)
				{
				case NET:
					theProperties.setProperty("server", theServerTextField.getText()); //$NON-NLS-1$
					theProperties.setProperty("directory", theDirectoryTextField.getText()); //$NON-NLS-1$
					theProperties.setProperty("login", theLoginTextField.getText()); //$NON-NLS-1$
					theProperties.setProperty("password", new String(thePasswordTextField.getPassword())); //$NON-NLS-1$
					break;

				case FILE:
					theProperties.setProperty("path", thePathTextField.getText()); //$NON-NLS-1$
					break;
				}
			}
		}
		super.setVisible(thisFlag);
	}
}
